package cn.swenty.backstage.framework.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;


public class CallUtil {
	// 用于匹配手机号码
	private final static String REGEX_MOBILEPHONE = "^0?1[34578]\\d{9}$";

	// 用于匹配固定电话号码
	private final static String REGEX_FIXEDPHONE = "^(010|02\\d|0[3-9]\\d{2})?\\d{6,8}$";

	// 用于获取固定电话中的区号
	private final static String REGEX_ZIPCODE = "^(010|02\\d|0[3-9]\\d{2})\\d{6,8}$";

	private static Pattern PATTERN_MOBILEPHONE;
	private static Pattern PATTERN_FIXEDPHONE;
	private static Pattern PATTERN_ZIPCODE;

	static {
		PATTERN_FIXEDPHONE = Pattern.compile(REGEX_FIXEDPHONE);
		PATTERN_MOBILEPHONE = Pattern.compile(REGEX_MOBILEPHONE);
		PATTERN_ZIPCODE = Pattern.compile(REGEX_ZIPCODE);
	}
	
	
	/**
	 *进线 
	 */
	public final static String CALL_TYPE_IN = "0";
	
	

	/**
	 *外呼
	 */
	public final static String CALL_TYPE_OUT = "1";
	

	/**
	 * 判断是否为手机号码
	 * 
	 * @param number
	 *            手机号码
	 * @return
	 */
	public static boolean isCellPhone(String number) {
		if(StringUtil.isNull(number)){
			return false;
		}
		Matcher match = PATTERN_MOBILEPHONE.matcher(number);
		return match.matches();
	}

	/**
	 * 判断是否为固定电话号码
	 * 
	 * @param number
	 *            固定电话号码
	 * @return
	 */
	public static boolean isFixedPhone(String number) {
		Matcher match = PATTERN_FIXEDPHONE.matcher(number);
		return match.matches();
	}

	/**
	 * 获取固定号码号码中的区号
	 * 
	 * @param strNumber
	 * @return
	 */
	public static String getZipFromHomephone(String strNumber) {
		Matcher matcher = PATTERN_ZIPCODE.matcher(strNumber);
		if (matcher.find()) {
			return matcher.group(1);
		}

		return null;
	}

	/**
	 * 检查号码类型，手机号码去掉0，座机在区号后加"-",无区号默认添加上海区号
	 * 
	 * @param String
	 * @return String
	 */
	public static String checkNumber(String _number,String provinceCode) {
		String number = _number;
		String rtNum = null;

		if (number != null && number.length() > 0) {
			if (isCellPhone(number)) {
				// 如果手机号码以0开始，则去掉0
				if (number.charAt(0) == '0') {
					number = number.substring(1);
				}
				rtNum = number;
			} else if (isFixedPhone(number)) {
				// 获取区号
				String zipCode = getZipFromHomephone(number);
				if (zipCode == null) {
					zipCode = provinceCode+"-" + number;
				} else {
					zipCode = _number.substring(0, zipCode.length())
							+ "-"
							+ number.substring(zipCode.length(),
									_number.length());
				}
				rtNum = zipCode;
			}else{
				rtNum = _number;
			}
		}

		return rtNum;
	}
	
	public static String replacePhone(String phone) {
		if(StringUtil.isNull(phone)){
			return "";
		}
		StringBuffer sb = new StringBuffer(phone);
		sb = sb.reverse();
		if (isCellPhone(phone)) {
			sb = new StringBuffer(StringUtils.overlay(sb.toString(), "****", 4,
					8));
		} else {
			try {
				String[] phones=sb.toString().split("-");
				if(phones.length==2){
					if (phone.split("-")[1].length() == 7) {
						sb = new StringBuffer(StringUtils.overlay(sb.toString(),
								"****", 3, 7));
					} else {
						sb = new StringBuffer(StringUtils.overlay(sb.toString(),
								"****", 4, 8));
					}
				}else{
					phone=phones[1]+"-"+phones[2];
					if (phone.split("-")[0].length() == 7) {
						sb = new StringBuffer(StringUtils.overlay(phone,
								"****", 3, 7));
					} else {
						sb = new StringBuffer(StringUtils.overlay(phone,
								"****", 4, 8));
					}
					sb.insert(0, (phones[0]+"-"));
				}
				
			} catch (Exception e) {
				sb = new StringBuffer(StringUtils.overlay(sb.toString(),
						"****", 4, 8));
			}
		}
		sb.reverse();
		return sb.toString();
	}

}
